﻿using System;
using System.Collections.Generic;
using ZilLion.Core.ConfigManager.Manager;
using ZilLion.TaskManager.Core.SystemMonitor;
using ZilLion.TaskManager.Entity.Config;
using ZilLion.TaskManager.Entity.Response;

//using Log4Wrapper;

namespace ZilLion.TaskManager.Core.Infrastructure
{
    /// <summary>
    ///     启动类
    /// </summary>
    public class TaskManagerBootstrapper
    {
        public static List<BaseMonitor> MonitorsCache = new List<BaseMonitor>();
        public static Action<NodeServerResponse> NodeHeartBeatMonitorAction = null;

        public static void Run()
        {
            try
            {
                #region 命令接收开启

                CommandReceiver.Run();

                #endregion

                #region 监控开启

                MonitorsCache.Add(new TaskPerformanceMonitor());
                MonitorsCache.Add(new NodeHeartBeatMonitor(NodeHeartBeatMonitorAction));
                //MonitorsCache.Add(new TaskRecoverMonitor());
                //MonitorsCache.Add(new TaskStopMonitor());

                #endregion
            }
            catch (Exception ex)
            {
                CustomTaskLogger.Print(ex,
                    $"{ZilLionConfigurationManager.GetConfigInstance<TaskManagerCommonConfig>().ClientNo}节点启动时(开启命令循环和监控)出现致命错误！");
            }
        }

        public static void Stop()
        {
            try
            {
                CommandReceiver.Stop();

                #region 监控开启

                foreach (var monitor in MonitorsCache)
                {
                    monitor.Stop();
                }
                MonitorsCache.Clear();
                #endregion
            }
            catch (Exception ex)
            {
                CustomTaskLogger.Print(ex,
                    $"{ZilLionConfigurationManager.GetConfigInstance<TaskManagerCommonConfig>().ClientNo}节点关闭时出现致命错误！");
            }
        }
    }

    
}